const app = getApp();

Page({

  data: {
    student: null,
    imageList: []
  },


  formSubmit: function (e) {
    let _this = this;
    wx.uploadFile({
      url: 'https://dorm.dormassistant.wang:8080/secondHand/addSecondHand', //仅为示例，非真实的接口地址
      filePath: _this.data.imageList[0],
      name: "image",
      formData: {
        userId: app.globalData.userId,
        title: e.detail.value.title,
        content: e.detail.value.content
      },
      header: {
        'content-type': "multipart/form-data"
      },
      success: (res) => {
        console.log(res);

        if (res.data.success)
          wx.showToast({
            title: '发布成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              setTimeout(function () {
                wx.navigateBack({
                  url: "./secondHandMarket/secondHandMarket"
                })
              }, 2000)
            }
          });

      }
    })
  },


  chooseImage: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#00000",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })

  },


  // 图片本地路径
  chooseWxImage: function (type) {
    var that = this;
    var imgsPaths = that.data.imgs;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      count: 1,
      success: function (res) {
        if (res.tempFiles[0].size > 750000) {
          wx.showToast({
            title: "图片过大",
            icon: '',
            duration: 2000
          });
          return;
        }
        that.setData({
          imageList: res.tempFilePaths
        })

      }
    })
  },

  /**
   * 图片预览
   */
  previewImage: function (e) {
    var current = e.target.dataset.src;

    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  }
})



